package ng.map.elements.objects;

import java.io.IOException;

import ng.graphics.Animation;
import ng.graphics.FrameBuffer;
import ng.lib.Vector2D;
import ng.map.MapObject;
import ng.map.MapObjectDynamic;
import ng.map.collision.CollisionBound;
import ng.map.collision.CollisionCircle;
import ng.map.gravitation.GravitationElementDynamic;
import ng.map.gravitation.GravitationElementStatic;

public class Laser extends MapObjectDynamic {
	private static FrameBuffer frames;
	private static CollisionBound collisionBoundHolder = new CollisionCircle(
			new Vector2D(0, 0), 5);

	static {
		try {
			frames = new FrameBuffer(new String[] {"data/textures/projectiles/laser/10x3/1.png"});
		} catch (IOException e) {
			System.err.println("An image isn't at the right place");
			e.printStackTrace();
		}
	}

	private static final int LIFETIME = 100; // Lebensdauer in ticks.

	private int life = 0;

	public Laser(Vector2D p) {
		super(collisionBoundHolder);
		this.animation = new Animation(frames, 1);
		this.position = p;
		this.mass = 1;
		this.center.x = 15;
		this.center.y = 6.5f;
	}

	@Override
	public void collides(MapObject mo) {
		super.collides(mo);
		if (mo instanceof Spacecraft)
			this.mapCallback.kill(this);
	}

	@Override
	public void tick() {
		super.tick();

		this.life++;
		if (this.life >= Laser.LIFETIME)
			this.mapCallback.kill(this);

		this.setAngle(this.speed.getAngle() + Math.PI / 2);
		this.angle += Math.PI / 2;

	}

}
